package com.itheima.health.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.SetmealDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Setmeal;
import com.itheima.health.service.SetmealService;
import lombok.extern.slf4j.Slf4j;

import com.github.pagehelper.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author ZhangXiXi
 * @Description TODO
 * @date 2021/1/12 17:00
 * @Version 1.0
 */
@Slf4j
@DubboService
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealDao setmealDao;
    @Override
    public void add(Integer[] checkgroupIds, Setmeal setmeal) {
        log.info("[套餐:添加]", checkgroupIds, setmeal);
        //插入基本信息  t_setmeal
        setmealDao.insert(setmeal);
        //插入关联关系 t_setmeal_checkgroup
        if (null != checkgroupIds && checkgroupIds.length > 0) {
            for (Integer checkGroupId : checkgroupIds) {
                setmealDao.insertSetmealAndCheckGroup(setmeal.getId(), checkGroupId);
            }
        }
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        log.info("[套餐:分页查询]", queryPageBean);
        //分页查询插件使用步骤
        //1 设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2 查询数据 返回值必须是page
        Page<Setmeal> page = setmealDao.selectByCondition(queryPageBean.getQueryString());
        //3 封装返回结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public long countByImg(String img) {
        return setmealDao.countByImg(img);
    }
}
